﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using SGUI;
using SDATA;

namespace SABI
{
    public partial class frm0413 : Form
    {
        public frm0413()
        {
            InitializeComponent();
        }

        private void frm0413_Load(object sender, EventArgs e)
        {
            LANG.TextForm(this, FRM.Lang);

            TopMost = true;
            TopMost = false;
            GUI.ChangeDateFormat(dtpWorkAt, FRM.SystemDateFormat);
            if(FRM.ConnectString=="")
            {
                MessageBox.Show("Bạn chưa cấu hình kết nối database!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                btnConfig_Click(sender, e);
            }
            else
            {
                LoadDATA();
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            if (CheckInput() == false) { return; }
            //MÃ HÓA PASSWORD
            string pass = txtPassword.Text.Replace("'", "''");
            if (pass.Trim() != "") { pass = EncryptSnip2code.Encrypt(pass); }
            string sql = "SELECT a.ID as ID_User,a.UserID,a.Password,c.ID as ID_Group, c.GroupList as ID_GroupList, c.DivisionList as ID_DivisionList FROM tb0074 a ";
            sql += "LEFT JOIN tb0071 b ON a.UserID=b.UserID ";
            sql += "LEFT JOIN tb0070 c ON b.GroupID = c.GroupID ";
            sql += "WHERE a.UserID='" + txtUsername.Text.Replace("'", "''") + "' AND a.Password='" + pass + "' AND a.Disabled=0";
            DataTable ListAcc = FRM.GetDataTable(sql);
            if(ListAcc.Rows.Count==1)
            {
                FRM.UserName = txtUsername.Text;
                FRM.UserPass = txtPassword.Text;
                FRM.UserRemember = cbRemember.Checked;
                FRM.IsLogin = true;
                FRM.DivisionID = cbxDivision.SelectedItem.ToString();
                FRM.GroupID = cbxGroupUser.SelectedItem.ToString();
                FRM.DateDue = dtpWorkAt.Value;
                FRM.ID_Group = "@" + ListAcc.Rows[0]["ID_Group"].ToString();
                FRM.ID_User = "@" + ListAcc.Rows[0]["ID_User"].ToString();
                FRM.ID_GroupList = ListAcc.Rows[0]["ID_GroupList"].ToString();
                FRM.ID_DivisionList = ListAcc.Rows[0]["ID_DivisionList"].ToString();
                Close();
            }
            else
            {
                MessageBox.Show("Sai tài khoản hoặc mật khẩu!","Thông báo",MessageBoxButtons.OK,MessageBoxIcon.Information);                
            }
        }

        private bool CheckInput()
        {
            if (txtUsername.Text.Trim() == "")
            {
                MessageBox.Show("Bạn chưa điền Tài khoản đăng nhập", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            else if(cbxGroupUser.Text=="")
            {
                MessageBox.Show("Tài khoản chưa thuộc nhóm nào nên không thể đăng nhập!!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            return true;
        }

        private void btnConfig_Click(object sender, EventArgs e)
        {
            Form frm0414 = new frm0414();
            frm0414.ShowDialog();
        }

        #region [CÁC HÀM XỬ LÝ]
        public void LoadDATA()
        {
            if (FRM.ConnectString == "") { return; }
            List<string> dtDivision = DATA.GetListSimple("SELECT DivisionID FROM tb0038", FRM.ConnectString);
            if (dtDivision.Count == 0) { return; }
            cbxDivision.DataSource = dtDivision;
            cbRemember.Checked = FRM.UserRemember;
            txtUsername.Text = FRM.UserName;
            txtPassword.Text = cbRemember.Checked ? FRM.UserPass : "";
            txtUsername_KeyUp(null, null);
            dtpWorkAt.Value = FRM.DateDue;
            GUI.ChangeDateFormat(dtpWorkAt, FRM.SystemDateFormat);
        }
        #endregion

        private void txtUsername_KeyUp(object sender, KeyEventArgs e)
        {
            if (FRM.ConnectString == "") { return; }
            List<string> dtGroupUser = DATA.GetListSimple("SELECT GroupID FROM tb0071 WHERE UserID='"+txtUsername.Text.Replace("'","''")+"' order by GroupID", FRM.ConnectString);
            if (dtGroupUser.Count == 0) { return; }
            cbxGroupUser.DataSource = dtGroupUser;
        }

        private void frm0413_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (FRM.IsLogin == false) { FRM.IsClose = true; }
        }

        private void frm0413_Shown(object sender, EventArgs e)
        {
            txtUsername.Focus();
            if (FRM.Lang == "vi") { pbxLang.Image = SABI.Properties.Resources.vi_flag; }
            else { pbxLang.Image = SABI.Properties.Resources.flag_uk_small; }
        }

        private void txtUsername_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                btnLogin_Click(null, null);
        }

        private void txtPassword_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                btnLogin_Click(null, null);
        }

        private void pbxLang_Click(object sender, EventArgs e)
        {
            if (FRM.Lang == "vi") {
                FRM.Lang = "en";
                pbxLang.Image = SABI.Properties.Resources.flag_uk_small; 
            }
            else {
                FRM.Lang = "vi";
                pbxLang.Image = SABI.Properties.Resources.vi_flag; 
            }
        }

    }
}
